from flask import request
import hashlib


from sale.apps.member.bll import login_bll
from sale.apps.member.bll import register_bll
from sale.apps.member.bll import select_bll
from sale.apps.member.bll import send_verify_code_bll
from sale.apps.member.bll import is_exist_bll
from sale.apps.member.bll import image_verify_code_bll
from sale.apps.member.bll import get_phone_bll
from sale.apps.member.bll import select_package_bll
from sale.apps.member.bll import create_order_bll
from sale.apps.member.bll import my_orders_bll
from sale.apps.member.bll import order_detail_bll
from sale.apps.member.dal import add_acquire_phone
from sale.apps.member.dal import add_package_info

# from sale.utils.loggit_decorate import logit
from sale.utils.loggit_decorate import auth


# def create_user():
#     request_data = request.form
#     password = request_data.get('password')
#     m = hashlib.md5()
#     m.update(password.encode(encoding='utf-8'))
#     md5_data = m.hexdigest()
#     ret = {
#         'phone': request_data.get('phone'),
#         'password': md5_data,
#     }
#     return ret


# @logit
def login():
    request_data = request.form
    username = request_data.get('name')
    password = request_data.get('password')
    m = hashlib.md5()
    m.update(password.encode(encoding='utf-8'))
    md5_data_one = m.hexdigest()
    ret = login_bll(username, md5_data_one)
    if ret.get('code') == 'fail':
        return ret
    headers = {'Set-Cookie': "token=%s" % ret.get('token')}

    return ret, 200, headers


# @logit
def register():
    request_data = request.form
    username = request_data.get('name')
    phone = request_data.get('phone')
    verify_code = request_data.get('verify_code')
    password = request_data.get('password')
    m = hashlib.md5()
    m.update(password.encode(encoding='utf-8'))
    md5_data_one = m.hexdigest()
    reg = register_bll(username, phone, verify_code, md5_data_one)
    return reg


@auth
def select(username):
    if username != request.user_info_view.get('username'):
        return {'msg': '只能获取自己的用户信息', 'code': 'auth_fail'}
    res = select_bll(username)
    return res


def is_exist_by_name():
    name = request.args.get('name')
    success, msg = is_exist_bll(name=name)
    if success:
        return {'msg': msg, 'code': "EXIST"}
    else:
        return {'msg': "", 'code': "NOT EXIST"}


def is_exist_by_phone():
    phone = request.args.get('phone')
    success, msg = is_exist_bll(phone=phone)
    if success:
        return {'msg': msg, 'code': "EXIST"}
    else:
        return {'msg': "", 'code': "NOT EXIST"}


# @logit
def send_verify_code():
    phone = request.form.get('phone')
    success, msg = send_verify_code_bll(phone=phone)
    if not success:
        return {'msg': msg, "code": "fail"}
    # 调用第三方
    return {'msg': "发送成功", "code": "success"}


# @logit
def image_verify_code():
    get_image_verify_code = request.args.get('image_verify_code')
    #  图片验证逻辑
    recorde = image_verify_code_bll(get_image_verify_code)
    return recorde


def acquire_phone():
    request_data = request.form
    phone = request_data.get('phone')
    record = add_acquire_phone(phone)
    return {'msg': '获取成功', 'code': "success"}


# 价格管理图层
def admin_package_info():
    request_data = request.form
    original_price = request_data.get('original_price')
    discount_price = request_data.get('discount_price')
    record = add_package_info(original_price, discount_price)
    return {'msg': '修改成功', 'code': 'success'}


@auth
def pack_info_view():
    status, package_info = select_package_bll()
    if status:
        return {'msg': "success", 'code': 'success', 'packages': package_info}
    else:
        return {'msg': '请求失败', 'code': 'fail'}


@auth
def get_myself_phone():
    user_info = request.user_info_view
    ret = get_phone_bll(user_info.get('username'))
    return ret


@auth
def create_order_view():
    request_data = request.form
    phone = request_data.get('phone')
    package_id = request_data.get('package_id')
    user_id = request.user_info_view.get('userid')
    user_phone = request.user_info_view.get('user_phone')
    status, record = create_order_bll(
        phone=phone, package_id=package_id,
        user_id=user_id, user_phone=user_phone
    )
    if not status:
        return {'msg': '请求失败', 'code': 'fail'}
    return {'msg': '请求成功', 'code': 'success', 'orders': record}


def notify_order_view():
    request.data
    pass


@auth
def my_orders_view():
    user_phone = request.user_info_view.get('user_phone')
    status, record = my_orders_bll(user_phone=user_phone)
    if not status:
        return {"msg": record, "code": "fail"}
    return {"msg": "查询成功", "code": "success", "order_info": record}


@auth
def order_detail_view():
    order_no = request.args.get("order_no")
    record = order_detail_bll(order_no)
    return {"msg": "查询成功", "code": "success", "order_detail": record}
